

clear all
	
cd "${data}"	

use "d1.dta", clear 


*keep respondent partyorder election party IMD5031_
keep election party IMD5031_ IMD5000_ country
duplicates drop


* cabinet information is missing in module 1, they are coded as missing. So, here we fix them based on data from ParlGov (https://parlgov.fly.dev/data/cabinets). We assign 1 to parties who are in government (the number does not matter as long as it is not 0), and 0 to those who were not in government.

* BELF1999 (coalition), BELW1999 (coalition)

replace IMD5031_ = 1 if IMD5000_ == 560018 & election=="BELF1999" // AGL-Gr	Agalev – Groen
replace IMD5031_ = 1 if IMD5000_ == 560013 & election=="BELW1999" // Ecolo	Écologistes Confédérés pour l'organisation de luttes originales
replace IMD5031_ = 1 if IMD5000_ == 560016 & election=="BELW1999" // PRL	Parti Réformateur Libéral
replace IMD5031_ = 1 if IMD5000_ == 560002 & election=="BELW1999" // PS Parti Socialiste	Socialist Party [Francophone]
replace IMD5031_ = 1 if IMD5000_ == 560004 & election=="BELF1999" // PVV|VLD	Partij voor Vrijheid en Vooruitgang | Vlaamse Liberalen en Democraten
replace IMD5031_ = 1 if IMD5000_ == 560005 & election=="BELF1999" // SP	Socialistische Partij

replace IMD5031_ = 0 if IMD5000_ == 560021 & election=="BELW1999"
replace IMD5031_ = 0 if IMD5000_ == 560023 & election=="BELW1999"
replace IMD5031_ = 0 if IMD5000_ == 560003 & election=="BELF1999"
replace IMD5031_ = 0 if IMD5000_ == 560012 & election=="BELF1999"
replace IMD5031_ = 0 if IMD5000_ == 560015 & election=="BELF1999"


*CANADA 1997

replace IMD5031_ = 1 if IMD5000_ == 1240001 & election=="CAN_1997"
replace IMD5031_ = 0 if IMD5000_ == 1240007 & election=="CAN_1997"
replace IMD5031_ = 0 if IMD5000_ == 1240006 & election=="CAN_1997"
replace IMD5031_ = 0 if IMD5000_ == 1240003 & election=="CAN_1997"
replace IMD5031_ = 0 if IMD5000_ == 1240004 & election=="CAN_1997"


*CHE_1999 (coalition)

replace IMD5031_ = 1 if IMD5000_ == 7560024 & election=="CHE_1999" // FDP-PRD	Freisinnig-Demokratische Partei der Schweiz – Parti Radical-Democratique Suisse	Radical Democratic Party
replace IMD5031_ = 1 if IMD5000_ == 7560003 & election=="CHE_1999" // KK/CVP	Katholische Konservative / Christlichdemokratische Volkspartei – Conservateurs catholiques  /  Parti démocrate-chrétien	Catholic Conservative / Christian Democratic People's Party
replace IMD5031_ = 1 if IMD5000_ == 7560002 & election=="CHE_1999" // SP-PS	Sozialdemokratische Partei der Schweiz – Parti Socialiste Suisse	Social Democratic Party of Switzerland
replace IMD5031_ = 1 if IMD5000_ == 7560001 & election=="CHE_1999" // SVP-UDC	Schweizerische Volkspartei – Union Démocratique du Centre	Swiss People's Party
replace IMD5031_ = 0 if IMD5000_ == 7560005 & election=="CHE_1999" 


*DEU_1998 (coalition)

replace IMD5031_ = 1 if IMD5000_ == 2760005 & election=="DEU_1998" // B90/Gru	Bündnis 90 / Die Grünen	Alliance 90 / Greens
replace IMD5031_ = 1 if IMD5000_ == 2760004 & election=="DEU_1998" // SPD	Sozialdemokratische Partei Deutschlands	Social Democratic Party of Germany
replace IMD5031_ = 0 if IMD5000_ == 2760002 & election=="DEU_1998" 
replace IMD5031_ = 0 if IMD5000_ == 2760003 & election=="DEU_1998" 
replace IMD5031_ = 0 if IMD5000_ == 2760006 & election=="DEU_1998" 
replace IMD5031_ = 0 if IMD5000_ == 2760007 & election=="DEU_1998" 


*ESP_1996 (single paarty)
replace IMD5031_ = 1 if IMD5000_ == 7240002 & election=="ESP_1996"  // AP-P	Alianza-Partido Popular	People's Alliance-Party
replace IMD5031_ = 0 if IMD5000_ == 7240001 & election=="ESP_1996"  
replace IMD5031_ = 0 if IMD5000_ == 7240004 & election=="ESP_1996"  
replace IMD5031_ = 0 if IMD5000_ == 7240003 & election=="ESP_1996"  
replace IMD5031_ = 0 if IMD5000_ == 7240005 & election=="ESP_1996"  


*ESP_2000 (single party)
replace IMD5031_ = 1 if IMD5000_ == 7240002 & election=="ESP_2000" // AP-P	Alianza-Partido Popular	People's Alliance-Party
replace IMD5031_ = 0 if IMD5000_ == 7240001 & election=="ESP_2000"  
replace IMD5031_ = 0 if IMD5000_ == 7240004 & election=="ESP_2000"  
replace IMD5031_ = 0 if IMD5000_ == 7240003 & election=="ESP_2000"  
replace IMD5031_ = 0 if IMD5000_ == 7240005 & election=="ESP_2000"  


* FRA_2017: only two parties are available for LR perception question. The fieldwork has ended in the 30th day following the election. 


*GBR_1997 (single party)

replace IMD5031_ = 1 if IMD5000_ == 8260002 & election=="GBR_1997"   // Labour
replace IMD5031_ = 0 if IMD5000_ == 8260001 & election=="GBR_1997" // Conservative
replace IMD5031_ = 0 if IMD5000_ == 8260003 & election=="GBR_1997" // Liberal Democrats
replace IMD5031_ = 0 if IMD5000_ == 8260004 & election=="GBR_1997" // SNP
replace IMD5031_ = 0 if IMD5000_ == 8260006 & election=="GBR_1997" // Plaid Cymru


*ISL_1999 (coaltion)

replace IMD5031_ = 1 if IMD5000_ == 3520003 & election=="ISL_1999"   // F	Framsóknarflokkurinn	Progressive Party
replace IMD5031_ = 1 if IMD5000_ == 3520001 & election=="ISL_1999"   // Sj	Sjálfstæðisflokkurinn	Independence Party
replace IMD5031_ = 0 if IMD5000_ == 3520002 & election=="ISL_1999"   
replace IMD5031_ = 0 if IMD5000_ == 3520004 & election=="ISL_1999"   
replace IMD5031_ = 0 if IMD5000_ == 3520005 & election=="ISL_1999"   


*NLD_1998 (coalition)
replace IMD5031_ = 1 if IMD5000_ == 5280004 & election=="NLD_1998"   // D66	Democraten 66	Democrats 66
replace IMD5031_ = 1 if IMD5000_ == 5280002 & election=="NLD_1998"   // PvdA	Partij van de Arbeid	Labour Party
replace IMD5031_ = 1 if IMD5000_ == 5280003 & election=="NLD_1998"   // VVD	Volkspartij voor Vrijheid en Democratie	People's Party for Freedom and DemocracyParty
replace IMD5031_ = 0 if IMD5000_ == 5280001 & election=="NLD_1998"   
replace IMD5031_ = 0 if IMD5000_ == 5280005 & election=="NLD_1998"   
replace IMD5031_ = 0 if IMD5000_ == 5280006 & election=="NLD_1998"   
replace IMD5031_ = 0 if IMD5000_ == 5280026 & election=="NLD_1998"   


*NOR_1997: 

replace IMD5031_ = 1 if IMD5000_ == 5780007 & election=="NOR_1997"   // KrF	Kristelig Folkeparti	Christian Democratic Party
replace IMD5031_ = 1 if IMD5000_ == 5780006 & election=="NOR_1997"   // Sp	Senterpartiet	Centre PartyDemocratic Party

* NOT ASKED: V	Venstre	Liberal Party of Norway (in government)
replace IMD5031_ = 0 if IMD5000_ == 5780001 & election=="NOR_1997"   
replace IMD5031_ = 0 if IMD5000_ == 5780003 & election=="NOR_1997"   
replace IMD5031_ = 0 if IMD5000_ == 5780002 & election=="NOR_1997"   
replace IMD5031_ = 0 if IMD5000_ == 5780004 & election=="NOR_1997"   



*NZL_1996

replace IMD5031_ = 1 if IMD5000_ == 5540001 & election=="NZL_1996"   // NP	National Party
replace IMD5031_ = 1 if IMD5000_ == 5540003 & election=="NZL_1996"   // NZFP	New Zealand First Party
replace IMD5031_ = 0 if IMD5000_ == 5540002 & election=="NZL_1996" 
replace IMD5031_ = 0 if IMD5000_ == 5540009 & election=="NZL_1996" 
replace IMD5031_ = 0 if IMD5000_ == 5540004 & election=="NZL_1996" 
replace IMD5031_ = 0 if IMD5000_ == 5540037 & election=="NZL_1996" 



* SWE_1998 (single party)

replace IMD5031_ = 1 if IMD5000_ == 7520001 & election=="SWE_1998"   // SAP	Socialdemokraterna
replace IMD5031_ = 0 if IMD5000_ == 7520002 & election=="SWE_1998" 
replace IMD5031_ = 0 if IMD5000_ == 7520007 & election=="SWE_1998" 
replace IMD5031_ = 0 if IMD5000_ == 7520004 & election=="SWE_1998" 
replace IMD5031_ = 0 if IMD5000_ == 7520006 & election=="SWE_1998" 
replace IMD5031_ = 0 if IMD5000_ == 7520003 & election=="SWE_1998" 



*USA_2004
replace IMD5031_ = 1 if IMD5000_ == 8400001 & election=="USA_2004"   // Republican Party
replace IMD5031_ = 0 if IMD5000_ == 8400002 & election=="USA_2004"  
replace IMD5031_ = 0 if IMD5000_ == 8400004 & election=="USA_2004"  

*USA_2008
replace IMD5031_ = 1 if IMD5000_ == 8400002 & election=="USA_2008"   // Democratic Party
replace IMD5031_ = 0 if IMD5000_ == 8400001 & election=="USA_2008"  


*USA_2012
replace IMD5031_ = 1 if IMD5000_ == 8400002 & election=="USA_2012"   // Democratic Party
replace IMD5031_ = 0 if IMD5000_ == 8400001 & election=="USA_2012"  


*USA_2016
replace IMD5031_ = 1 if IMD5000_ == 8400001 & election=="USA_2016"   // Republican Party
replace IMD5031_ = 0 if IMD5000_ == 8400002 & election=="USA_2016"  


*USA_2020
replace IMD5031_ = 1 if IMD5000_ == 8400002 & election=="USA_2020"   // Democratic Party
replace IMD5031_ = 0 if IMD5000_ == 8400001 & election=="USA_2020"  
 
 
* adjustments for missing:
replace IMD5031_ = 0 if IMD5000_ == 2760008 & election=="DEU12002" // The Republicans
replace IMD5031_ = 0 if IMD5000_ == 2760024 & election=="DEU12002" // Party of the Rule of Law Offensive

replace IMD5031_ = 0 if IMD5000_ == 7520009 & election=="SWE_2006" // Feminist Party

 
replace IMD5031_ = . if IMD5031_==999

 
gen ingovernment=.	// if the party has more than 0 portfolio, it is in the government 
replace ingovernment = 1 if IMD5031_>0 & IMD5031_!=.
replace ingovernment = 0 if IMD5031_==0


bysort election : egen numberoftotalportfolios = sum(IMD5031_) // this is the total number of portfolios that exist in the country at a given election survey


gen sizeoftheinparty = IMD5031_ if ingovernment==1 //  the number of portfolio of an in-government party 


gen coalitionpartner = 0 // if 0, it is a single-party government (at the party level)
replace coalitionpartner = 1 if numberoftotalportfolios > sizeoftheinparty // if 1, it is a coalition government  (at the party level)


bysort election: egen sum_xxx = sum(coalitionpartner) // this give the number of parties in a government. If more than 1, then code it as coalition, if 0 it is not a coalition. This is at the election level and not for specific parties 
replace sum_xxx = 1 if sum_xxx==2 | sum_xxx==3 | sum_xxx==4 | sum_xxx==5 | sum_xxx==6

rename sum_xxx coalitiongovernment



gen singleparty = .
replace singleparty = 1 if ingovernment==1 & coalitiongovernment==0
replace singleparty = 0 if ingovernment==0 
replace singleparty = 0 if ingovernment==1 & coalitiongovernment==1

replace singleparty = 0 if election=="BELW2019" 


/* single-party government parties:

0360003. AUS - Australian Labor Party   
           1240001. CAN - Liberal Party 
      1240002. CAN - Conservative Party 
        2080001. DNK - Social Democrats 
3000001. GRC - Pan-Hellenic Socialist M 
           3000002. GRC - New Democracy 
               3720002. IRL - Fine Gael 
          5540001. NZL - National Party 
            5540002. NZL - Labour Party 
         6200002. PRT - Socialist Party 
7240001. ESP - Spanish Socialist Worker 
          7240002. ESP - People's Party 
7520001. SWE - Sweden's Social Democrat 
           8260001. GBR - Conservatives 
                   8260002. GBR - Labor 
        8400001. USA - Republican Party 
        8400002. USA - Democratic Party 

*/

*This dataset needs to be merged with the main dataset. No need for sizeoftheinparty, drop it. 

keep election country IMD5000_ coalitiongovernment singleparty
drop if IMD5000_==.
duplicates drop 
gen key_coalition = election+"_"+string(IMD5000_)

save "coalition.dta", replace 
